

import java.io.*;
import java.util.Vector;

import junit.framework.TestCase;
import reittiopas.*;

public class TestXMLParser extends TestCase {
	
	XMLParser xmlParser;
	FileInputStream is1;
	
	public void setUp() { 
		try {
			is1 = new FileInputStream("testdata/exampleRoute1.xml");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		xmlParser = new XMLParser();

		print(getContents(new File("testdata/exampleRoute1.xml")));
	}
	
	
	public void testParseRoutes() {
		Vector routes = xmlParser.getRoutes(is1);
		print(routes.toString());
		assertEquals(3, routes.size());
		Vector subs = ((Route)routes.elementAt(0)).getSubRoutes();
		assertEquals(5, subs.size());
		SubRoute sub1 = ((SubRoute)subs.elementAt(0));
		assertEquals(ReittiopasDefinitions.Transportation.WALK, sub1.getTransportation());
		SubRoute sub2 = ((SubRoute)subs.elementAt(1));
		assertEquals(ReittiopasDefinitions.Transportation.BUS, sub2.getTransportation());
		assertEquals("73N", sub2.getVehicleNumber());
	}
	
	
	public void testParseRouteSpecs() {
		Vector routes = xmlParser.getRoutes(is1);
		Route r1 = (Route)routes.elementAt(0);
		assertEquals("10km 507m", r1.getTotalDistance());
		assertEquals("00:46", r1.getTotalTime());
		assertEquals("00:42", r1.getDestinationLocation().getArrivalTime());
		assertEquals("23:55", r1.getStartLocation().getDepartureTime());
		
		SubRoute sr1 = (SubRoute)r1.getSubRoutes().firstElement();
		assertEquals("00:03", sr1.getTotalTime());
		assertEquals("277m", sr1.getTotalDistance());
		assertEquals(ReittiopasDefinitions.Transportation.WALK, sr1.getTransportation());

		
		SubRoute sr2 = (SubRoute)r1.getSubRoutes().elementAt(1); //Bus route
		assertEquals("00:07", sr2.getTotalTime());
		assertEquals("4km 720m", sr2.getTotalDistance());
		assertEquals(ReittiopasDefinitions.Transportation.BUS, sr2.getTransportation());
		assertEquals("73N", sr2.getVehicleNumber());	
	}
	
	
	
	private void print(String text) {
		System.out.println(text);
	}

	
	/**
	 * for debugging
	 */
	private String getContents(File aFile) {
	    StringBuilder contents = new StringBuilder();
	    
	    try {

	      BufferedReader input =  new BufferedReader(new FileReader(aFile));
	      try {
	        String line = null; 

	        while (( line = input.readLine()) != null){
	          contents.append(line);
	          contents.append(System.getProperty("line.separator"));
	        }
	      }
	      finally {
	        input.close();
	      }
	    }
	    catch (IOException ex){
	      ex.printStackTrace();
	    }
	    
	    return contents.toString();
	  }


}
